#include "XSystem/XCore.h"

XWindow::XWindow(XResourcesManager *data)
{
	x_app = data->GetApp();
	x_data = data;

	x_static = false;
	x_visible = false;

	x_manualOverride = false;

	x_view = x_app->GetDefaultView();

	x_startDragX = 0;
    x_startDragY = 0;

	// Void button
	x_widgets["buttonsend"] = new XLabel(data, 0, 00, "");
}

// Change the visibility boolean
void XWindow::SetVisible()
{
    if (x_visible == false)
        x_visible = true;
    else
        x_visible = false;
}

// Change the visibility boolean
void XWindow::SetVisible(bool visible)
{
    x_visible = visible;
}

// \return if the window is visible
bool XWindow::visible()
{
	return x_visible;
}

// \return if the window is static
bool XWindow::isStatic()
{
	return x_static;
}

// Check if the mouse is inside the menu
bool XWindow::CheckPosition(int mouseX, int mouseY)
{
	if (x_visible)
	{
		if (mouseX > x_positionX && mouseX < x_positionX + x_width)
		{
			if (mouseY > x_positionY && mouseY < x_positionY + x_height)
			{
				return true;
			}
		}
	}

    return false;
}

// Drag the window
void XWindow::DragWindow(int mouseX, int mouseY)
{
    if (x_startDragX != 0)
    {
        // Set the base positions
        int moveX = mouseX - x_startDragX;
        int moveY = mouseY - x_startDragY;

        x_positionX += moveX;
        x_positionY += moveY;
		 
        x_startDragX = mouseX;
        x_startDragY = mouseY;

		x_view.Move(-moveX, -moveY);
    }
}

// Reset the window dragging
void XWindow::ResetDrag()
{
    x_startDragX = 0;
    x_startDragY = 0;

	// Reset widget position
	for(std::map<std::string, XWidget*>::const_iterator it = x_widgets.begin(); it != x_widgets.end(); ++it)
		it->second->Move(x_positionX, x_positionY);
}

// Draws the widgets
void XWindow::Draw()
{
	if (x_visible)
	{
		// View
		x_app->SetView(x_view);

		// Draws the background
		x_app->Draw(x_windowBackground);

		// Draws the widgets
		for(std::map<std::string, XWidget*>::const_iterator it = x_widgets.begin(); it != x_widgets.end(); ++it)
			it->second->Draw();
	}
}

// When user clicked somewhere, refresh
void XWindow::CheckState(const sf::Input &input)
{
	if (x_visible)
	{
		if (x_startDragX == 0)
		{
			x_startDragX = 0;
			x_startDragY = 0;
			bool isDragging = false;

			unsigned int mouseX = input.GetMouseX();
			unsigned int mouseY = input.GetMouseY();
			bool mousePressed = input.IsMouseButtonDown(sf::Mouse::Left);

			// Click on the dragging bar
			if (!x_static && mousePressed)
			{
				if (mouseX> (x_positionX) && mouseX < (x_positionX + x_width))
				{
					if (mouseY > (x_positionY) && mouseY < (x_positionY + 15))
					{
						// Clicked on close button
						if (mouseX > (x_positionX + x_width - 11) && mouseX < (x_positionX + x_width - 2) && mouseY > (x_positionY + 1) && mouseY < (x_positionY + 10))
						{
							x_visible = false;

						// Clicked on top (drag)
						} else {

							x_startDragX = mouseX;
							x_startDragY = mouseY;
							isDragging = true;
						}

						x_data->SetInt("movingWindow", 1);
					}
				}
			}

			// Check the state of the widgets
			for(std::map<std::string, XWidget*>::const_iterator it = x_widgets.begin(); it != x_widgets.end(); ++it)
			{
				it->second->CheckState(input);
			}

			// Check the focus of the widgets
			for(std::map<std::string, XWidget*>::const_iterator it = x_widgets.begin(); it != x_widgets.end(); ++it)
			{
				if (it->second->focus() == XG_FOC_GAINED)
				{
					for(std::map<std::string, XWidget*>::const_iterator it2 = x_widgets.begin(); it2 != x_widgets.end(); ++it2)
						it2->second->SetFocus(false);

					it->second->SetFocus(true);
				}
			}
		}
	}
}

// Check the events (User typing, pressing a key...)
void XWindow::CheckEvent(const sf::Event &Event)
{
	if (x_visible)
	{
		// Entering text event
		for(std::map<std::string, XWidget*>::const_iterator it = x_widgets.begin(); it != x_widgets.end(); ++it)
		{
			if (it->second->type() == XG_TYP_TEXTFIELD)
				it->second->CheckEvent(Event);
		}

		// Pressing tab
		if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Tab))
		{
			//
		}

		// Pressing enter
		if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Return))
		{
			x_widgets["buttonsend"]->SetState(XG_ST_LBUTTON_RELEASED);
			x_manualOverride = true;
		}
	}
}
